package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.swing.*;
import java.util.Date;
import java.util.List;
/**
* 攻略评论服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repsitory;

    @Autowired
    private MongoTemplate template;



    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        repsitory.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repsitory.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,StrategyComment.class,query,pageable);
    }

    @Override
    public void addComment(StrategyComment strategyComment) {
        // 设置时间
        strategyComment.setCreateTime(new Date());
        this.save(strategyComment);
    }

    @Override
    public void commentThumb(String cid, String uid) {
        // 获取当前评论的 点赞 id集合
        StrategyComment strategyComment = this.get(cid);
        List<String> thumbuplist = strategyComment.getThumbuplist();
        // 判断当前用户的id 是否存在于这个集合
        if (thumbuplist.contains(uid)){
            // 如果存在，说明已经点过赞了，这个操作是取消赞，点赞数量减一，移除这个id
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
            thumbuplist.remove(uid);
        }else {
            // 如果不存在，操作是点赞，点赞数加一，加上这个id
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
            thumbuplist.add(uid);
        }
        // 更新
        this.update(strategyComment);
    }



    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if(StringUtils.hasLength(strategyComment.getId())){
            //update
            this.update(strategyComment);
        }else{
            //save
            this.save(strategyComment);
        }
    }
}
